% reference MATLAB document help: 
%   topic: Fourier Analysis (Data Analysis Section)
%   topic: Fourier Analysis (Wavelets: A New Tool for Signal Analysis Section)
%   topic: Short-Time Fourier Analysis

choice = 1;
switch (choice)
    case 1:
        x = [4 3 7 -9 1 0 0 0]';
        y = fft(x);
        magnitude = abs(y)
        phases = angel(y);
        
    % tutorial of FFT analysis
    case 2:
        % example: Using FFT to Calculate Sunspot Periodicity
        %   topic: Fourier Analysis (Data Analysis Section)
        load sunspot.dat
        year = sunspot(:,1);
        wolfer = sunspot(:,2);
        plot(year,wolfer)
        title('Sunspot Data')
        
        Y = fft(wolfer);
        % The result of this transform is the complex vector Y. The magnitude 
        % of of Y squared is called the estimated power spectrum. A plot of 
        % the estimated power spectrum versus frequency is called a periodogram.
        
        % Because the first component of Y, which is simply the sum of the 
        % data, has a large magnitude, the following syntax removes it before 
        % generating the periodogram:
        %
        N = length(Y);
        Y(1) = [];
        power = abs(Y(1:N/2)).^2;
        nyquist = 1/2;
        freq = (1:N/2)/(N/2)*nyquist;
        plot(freq,power), grid on
        xlabel('cycles/year')
        title('Periodogram')
        
        % The frequency scale is in cycles/year, which is inconvenient because 
        % for estimating the period of one cycle in years. Therefore, plot 
        % the power versus period (where period = 1./freq) from 0 to 40 
        % years/cycle:period = 1./freq;
        plot(period,power), axis([0 40 0 2e7]), grid on
        ylabel('Power')
        xlabel('Period(Years/Cycle)')
    
        % In order to determine the cycle more precisely, use the following 
        % syntax:
        [mp,index] = max(power);
        period(index)
        % ans = 11.0769
        % This plot confirms the cyclical nature of sunspot activity,
        % which reaches a maximum about every 11 years.

    case 3:
        % tutorial: Magnitude and Phase of Transformed Data (matlab document)
        %  the unwrap function to remove phase jumps greater than  to their
        %  2*pi complement:
        t = 0:1/100:10-1/100;
        x = sin(2*pi*15*t) + sin(2*pi*40*t);
        y = fft(x); 
        m = abs(y);
        p = unwrap(angle(y));
        f = (0:length(y)-1)'*100/length(y);
        
        subplot(2,1,1), plot(f,m), 
        ylabel('Abs. Magnitude'), grid on
        subplot(2,1,2), plot(f,p*180/pi)
        ylabel('Phase [Degrees]'), grid on
        xlabel('Frequency [Hertz]')
        
    case 4:
        % @reference: tutorial: Short-Time Fourier Analysis
        % @reference: online ebook on short-time FFT:
        % http://mi.eng.cam.ac.uk/~ajr/SA95/node19.html

    case 5:
        % You may have noticed that wavelet analysis does not use a time-frequency 
        % region, but rather a time-scale region. For more information about 
        % the concept of scale and the link between scale and frequency, 
        % see "How to Connect Scale to Frequency?" in matlab document
        % refer: FAQ::Advanced Concepts(Wavelet Toolbox)
        %
    case 6:
end
        
        
        